<template>
  <div id="detail">
    <detail-nav-bar
      class="detail-nav"
      @titleClick="titleClick"
      ref="nav"
    ></detail-nav-bar>
    <scroll
      class="detail-content"
      :pull-up-load="true"
      :probe-type="3"
      ref="scroll"
      @scroll="contentScroll"
    >
      <detail-swiper :topImages="topImages"></detail-swiper>
      <detail-base-info :baseInfo="baseInfo"></detail-base-info>
      <detail-shop-info :shopInfo="shopInfo"></detail-shop-info>
      <detail-goods-info
        :detailInfo="detailInfo"
        @imageLoad="imageLoad"
      ></detail-goods-info>
      <detail-goods-params
        :paramsInfo="paramsInfo"
        ref="params"
      ></detail-goods-params>
      <detail-evaluete-info
        :evalueteInfo="evalueteInfo"
        ref="evaluete"
      ></detail-evaluete-info>
      <detail-recommend-goods
        :recommendGoodsList="recommendGoodsList"
        ref="recommend"
      ></detail-recommend-goods>
    </scroll>
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
    <back-top @click.native="backTopClick" v-if="isShowBackTop"></back-top>
  </div>
</template>

<script>
import DetailNavBar from "./childDetail/DetailNavBar.vue";
import DetailSwiper from "./childDetail/DetailSwiper.vue";
import DetailBaseInfo from "./childDetail/DetailBaseInfo.vue";
import DetailShopInfo from "./childDetail/DetailShopInfo.vue";
import DetailGoodsInfo from "./childDetail/DetailGoodsInfo.vue";
import DetailGoodsParams from "./childDetail/DetailGoodsParams.vue";
import DetailRecommendGoods from "./childDetail/DetailRecommendGoods.vue";
import DetailEvalueteInfo from "./childDetail/DetailEvalueteInfo.vue";
import DetailBottomBar from "./childDetail/DetailBottomBar.vue";

import Scroll from "@/components/common/scroll/Scroll";
import BackTop from "@/components/common/backtop/BackTop";

import { imgListenerMixin, backTopMixin } from "@/common/mixin";
import { debounce } from "@/common/utils";

import {
  getDetailGoods,
  getRecommendGoods,
  Goods,
  Shop,
  Params
} from "@/network/detail";

export default {
  name: "detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailGoodsParams,
    DetailEvalueteInfo,
    DetailRecommendGoods,
    DetailBottomBar,
    Scroll,
    BackTop
  },
  mixins: [imgListenerMixin, backTopMixin],
  data() {
    return {
      iid: null,
      topImages: [],
      baseInfo: {},
      shopInfo: {},
      detailInfo: {},
      paramsInfo: {},
      evalueteInfo: {},
      recommendGoodsList: [],
      themeTopYs: [], // 记录各个主题的offsetTop
      getThemeTopYs: null, // 接收对主题的防抖函数
      currentIndex: 0 // 记录nav滚动到某个位置时切换的主题的下标值
    };
  },
  created() {
    // 1.通过动态路由的方式获取到商品的iid
    // this.iid = this.$route.params.iid
    // 通过query的方式
    this.iid = this.$route.query.iid;
    // 2.请求详情商品数据函数
    this.getDetailGoods();
    // 3.请求推荐商品数据函数
    this.getRecommendGoods();

    // 4.给themeTopYs赋值,使用防抖
    this.getThemeTopYs = debounce(() => {
      this.themeTopYs.push(
        0,
        this.$refs.params.$el.offsetTop,
        this.$refs.evaluete.$el.offsetTop,
        this.$refs.recommend.$el.offsetTop
      );
      // hack做法，空间换时间
      // this.themeTopYs.push(Number.MAX_VALUE)
    });
  },
  mounted() {
    // 使用mixin混入的方式对推荐模块图片的加载事件处理
  },
  destroyed() {
    this.$bus.$off("itemImgLoad", this.itemImgListener);
  },
  methods: {
    /**
     * 事件处理模块
     */
    // 商品信息图片加载事件处理
    imageLoad() {
      this.$refs.scroll.refresh();
      // 在created中对商品详情图片数据进行防抖处理，在图片加载完成之后调用，获取各个部分的offsetTop
      this.getThemeTopYs();
    },
    // 顶部导航栏点击
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 300);
    },
    // 监听滚动，切换导航栏的主题切换
    contentScroll(position) {
      let positionY = -position.y;
      let length = this.themeTopYs.length;
      for (let i = 0; i < length; i++) {
        if (
          this.currentIndex !== i &&
          ((i < length - 1 &&
            positionY >= this.themeTopYs[i] &&
            positionY < this.themeTopYs[i + 1]) ||
            (i === length - 1 && positionY >= this.themeTopYs[i]))
        ) {
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }

      // 决定是否显示回到顶部的组件
      this.isShowBackTop = -position.y > 800;
    },
    //添加到购物车
    addToCart() {
      // 1. 获取商品在购物车里要展示的数据
      const product = {};
      product.image = this.topImages[0];
      product.title = this.baseInfo.title;
      product.desc = this.baseInfo.desc;
      product.price = this.baseInfo.realPrice;
      product.iid = this.iid;
      // 将商品提交到store的actions中
      // 1.直接使用this.$store.dispatch
      // 2.使用vuex属性mapActions  this.addCart(product).then
      this.$store.dispatch("addCart", product).then(res => {
        // console.log(res)
        this.$toast.show(res, 2000);
      });
    },

    /**
     * 网络请求模块
     */
    // 请求商品详情信息
    getDetailGoods() {
      getDetailGoods(this.iid).then(res => {
        console.log(res);
        let data = res.result;
        // 获取顶部轮播图数据
        this.topImages = data.itemInfo.topImages;
        // 获取商品的基本信息
        this.baseInfo = new Goods(
          data.itemInfo,
          data.columns,
          data.shopInfo.services
        );
        // 获取商家店铺信息
        this.shopInfo = new Shop(data.shopInfo);
        // 获取商品详情信息
        this.detailInfo = data.detailInfo;
        // 获取商品参数信息
        this.paramsInfo = new Params(
          data.itemParams.info,
          data.itemParams.rule
        );
        // 获取用户评价信息
        this.evalueteInfo = data.rate;
      });
    },
    // 请求详情页底部的推荐商品
    getRecommendGoods() {
      getRecommendGoods().then(res => {
        this.recommendGoodsList = res.data.list;
        // 保存到vuex
        this.$store.commit("saveRecommendGoods", this.recommendGoodsList);
      });
    }
  }
};
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 9;
  height: 100vh;
  background-color: #fff;
}
.detail-content {
  /* position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
  overflow: hidden; */
  height: calc(100% - 44px - 49px);
  overflow: hidden;
}
</style>
